﻿'use strict';
!function () {

    var Emails = function () {
        if (this instanceof Emails) {
            this.init = function () {
                this.getInitVal();
                this.eventBind();
            };
            this.eventBind = function () {
                var $this = this;
                $('#bind_email_btn').on('click', function () {
                    if ($this.checkEmailInput()) {
                        $this.saveEmailChange();
                    }
                });
            };
            //获得用户的默认初始值
            this.getInitVal = function () {
                var $this = this;
				var nickname = $('[name="email"]')[0].value;
				var mobile = $('[name="email"]')[1].value;
				var truename = $('[name="email"]')[2].value;
				var email = $('[name="email"]')[3].value;
				var qq = $('[name="email"]')[4].value;
				var wechat = $('[name="email"]')[5].value;

				if (nickname != "") {
					$('[name="email"]')[0].disabled = true;
					$('[name="email"]')[0].readOnly = true;
				}
				if (mobile != "") {
					$('[name="email"]')[1].disabled = true;
					$('[name="email"]')[1].readOnly = true;
				}
				if (truename != "") {
					$('[name="email"]')[2].disabled = true;
					$('[name="email"]')[2].readOnly = true;
				}
				if (email != "") {
					$('[name="email"]')[3].disabled = true;
					$('[name="email"]')[3].readOnly = true;
				}
				if (qq != "") {
					$('[name="email"]')[4].disabled = true;
					$('[name="email"]')[4].readOnly = true;
				}
				if (wechat != "") {
					$('[name="email"]')[5].disabled = true;
					$('[name="email"]')[5].readOnly = true;
				}
				//用户资料都不为空删除修改按钮
				if (nickname != "" && mobile != "" && truename != "" && email != "" && qq != "" && wechat != "") {
					$("#bind_email_btn").parent().remove();
				}
            };
            this.checkEmailInput = function () {
                var $this = this
                    , re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/
                    , checkQ = /^[1-9][0-9]{4,10}$/
                    , inputArr = $('[name="email"]');
                if (inputArr[3].value === '') {
                    $this.tip('提示', '用户邮箱不能为空，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[3].focus();
                        }, 400);
                    });
                    return false;
                } else if (inputArr[4].value === '') {
                    $this.tip('提示', '用户QQ不能为空，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[4].focus();
                        }, 400);
                    });
                    return false;
                } else if (inputArr[0].value === '') {
                    $this.tip('提示', '用户昵称不能为空，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[0].focus();
                        }, 400);
                    });
                    return false;
                } else if (inputArr[1].value === '') {
                    $this.tip('提示', '用户手机不能为空，请核实！', '确定', function (data) { });
                    return false;
                } else if (inputArr[2].value === '') {
                    $this.tip('提示', '真实姓名不能为空，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[2].focus();
                        }, 400);
                    });
                    return false;
                } else if (!re.test(inputArr[3].value)) {
                    $this.tip('提示', '邮箱地址格式不正确，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[3].focus();
                        }, 400);
                    });
                    return false;
                } else if (!checkQ.test(inputArr[4].value) && $("#txtQQ").attr("disabled") != "disabled") {
                    $this.tip('提示', 'QQ格式不正确，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[4].focus();
                        }, 400);
                    });
                    return false;
                } else if (inputArr[1].value !== '' && !/^((\+?86)|(\(\+86\)))?1[3|4|5|7|8]\d{9}$/.test(inputArr[1].value) && $("#txtPhone").attr("disabled") != "disabled") {
                    $this.tip('提示', '手机号码格式不正确，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[1].focus();
                        }, 400);
                    });
                    return false;
                }
                else if (inputArr[5].value !== "" && !/^[a-z\d][\d\w\-]{5,19}$/i.test(inputArr[5].value) && $("#txtWeChat").attr("disabled") != "disabled") {
                    $this.tip('提示', '微信格式不正确，请核实！', '确定', function (data) {
                        setTimeout(function () {
                            inputArr[5].focus();
                        }, 400);
                    });
                    return false;
                }
                return 1;
            };
            //保存用户的邮箱修改
            this.saveEmailChange = function () {
                var $this = this , inputArr = $('[name="email"]');

                $.ajax({
                    url: U('member/info'),
                    type: 'post',
                    dataType: 'json',
                    data: {
                        qq: inputArr[4].value,
                        email: inputArr[3].value,
                        nickname: inputArr[0].value,
                        truename: inputArr[2].value,
                        mobile: inputArr[1].value,
                        wechat: inputArr[5].value
                    },
                    success: function (data) {
                        var dt = (typeof data === 'string') ? eval('(' + data + ')') : data;
                        if (dt.status) {
                            $this.tip('提示', dt.info + ",2秒后自动刷新页面", '关闭', function (data) { });
                            setTimeout(function () { window.location.reload(); }, 2000);
                        } else {
                            $this.tip('提示', dt.info, '关闭', function (data) { });
                        }
                    },
                    error: function (err) {
                        $this.tip('提示', '保存数据失败！', '关闭', function (data) { });
                    }
                });
            };
            this.tip = function (title, content, okVal, ok) {
                dialog({ title: title, content: content, ok: ok || function () { }, okValue: okVal || "关闭" }).show()
            };
        } else {
            new Emails();
        };
    };
    var emails = new Emails();
    emails.init();
}();